﻿Imports System.Runtime.CompilerServices
Imports System.IO

'created by : Sum
'created date : 2/10/2011
'description : stream exgtension

Public Module streamExtension
    <Extension()> _
    Public Function AsString(ByVal FileStream As Stream) As String
        Dim reader As New StreamReader(FileStream)
        Return reader.ReadToEnd
    End Function

    <Extension()> _
    Public Sub CopyTo(ByVal input As Stream, ByVal output As Stream)
        Dim buffer(1024) As Byte
        Dim count As Integer = buffer.Length

        While count > 0
            count = input.Read(buffer, 0, count)
            If count > 0 Then
                output.Write(buffer, 0, count)
            End If
        End While

    End Sub
    <Extension()> _
    Public Sub writeFile(ByVal text As String, ByVal FileStream As Stream)
        Dim SwFromFile As StreamWriter = New StreamWriter(FileStream)
        SwFromFile.Write(text)
        SwFromFile.Flush()
        SwFromFile.Close()
    End Sub


    <Extension()> _
    Public Function ConvertToByte(ByVal stream As Stream) As Byte()
        Dim content As Byte()
        Using br As New BinaryReader(stream)
            content = br.ReadBytes(500000)
            br.Close()
        End Using
        Return content
    End Function

End Module






